package com.hxzy;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

public class ReadProperties {
    @Test
    public void  test(){
        String path="jdbc.properties";
        Properties load = load(path);
        //读取properties中所有的键
        Enumeration<?> enumeration = load.propertyNames();
        while (enumeration.hasMoreElements()){
            String key = (String) enumeration.nextElement();
            String value = load.getProperty(key);
        }

    }
    public Properties load(String path){
        //通过类加载器，从本地文件路径加载jdbc,properties文件
        InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream(path);
        //继承了HashTable的类，HashTable实现了Map接口
        //properties 以键值对的方式保存元素
        Properties properties=new Properties();
        try {
           // 加载配置文件
            properties.load(resourceAsStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return properties;
    }
}
